package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GroupUserAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGroupUserAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GroupUserAddLogic {
	return &GroupUserAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分组好友列表
func (l *GroupUserAddLogic) GroupUserAdd(in *userrpc.GroupUserAddRequest) (*userrpc.GroupUserAddResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	groupUser := mysql.GroupUser{
		UserId:   in.UserId,
		FriendId: in.FriendId,
		GroupId:  in.GroupId,
	}

	mysql.DB.Debug().Create(&groupUser)
	return &userrpc.GroupUserAddResponse{Id: int64(groupUser.ID)}, nil
}
